package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CategoryBrandAttributesRelationMapper;
import com.ztgf.commodity.models.dto.CategoryBrandAttributesRelationDTO;
import com.ztgf.commodity.models.dto.CommodityLabelDTO;
import com.ztgf.commodity.models.service.ICategoryBrandAttributesRelationService;
import com.ztgf.commodity.models.service.ICommodityLabelService;
import com.ztgf.commodity.models.service.ICommodityUnitService;
import com.ztgf.commodity.models.vo.CategoryBrandAttributesVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.isNull;

/*
 * @author: chenyu.wang
 * @date: 2020-09-09
 * @description:
 */
@Slf4j
@DubboService
public class CategoryBrandAttributesRelationServiceImpl extends ServiceImpl<CategoryBrandAttributesRelationMapper, CategoryBrandAttributesRelationDTO> implements ICategoryBrandAttributesRelationService {

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private ICommodityUnitService iCommodityUnitService;
    @DubboReference
    private ICommodityLabelService commodityLabelService;


    /*
     * @description:根据类目Id和品牌id删除
     * @author: chenyu.wang
     * @date: 2020-09-10
     * @return:
     */
    @Override
    public void deleteByTwoId(Long categoryId, Long brandId) {
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("brand_id", brandId);
        CategoryBrandAttributesRelationDTO dto = this.selectOne(wrapper);
        if (dto == null) {
            log.info("暂无此类目品牌属性关联,这是添加新的!");
        } else {
            boolean delete = this.delete(wrapper);
            if (delete) {
                log.info("删除类目品牌属性关系成功!");
            } else {
                throw new RuntimeException("修改类目失败!");
            }
        }
    }

    @Override
    public void deleteByCategoryId(Long categoryId) {
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<CategoryBrandAttributesRelationDTO> list = this.selectList(wrapper);
        if (isNull(list)) {
            log.info("该类目没有品牌属性关系!");
        } else {
            boolean delete = this.delete(wrapper);
            if (delete) {
                log.info("根据类目id删除品牌属性成功!");
            } else {
                throw new RuntimeException("新增类目下级失败!");
            }
        }
    }

    @Override
    public List<CategoryBrandAttributesVO> queryByCategoryId(Long categoryId) {
        return baseMapper.queryByCategoryId(categoryId);
    }

    @Override
    public void addUnitidsByBrandAndCategory(Long brandId, Long categoryId) {
        //查询是否存在
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("brand_id", brandId).eq("category_id", categoryId);
        CategoryBrandAttributesRelationDTO categoryBrandAttributesRelationDTO = this.selectOne(wrapper);

        String unitids = iCommodityUnitService.getUnitids();//获取所有单位的ids
        if (categoryBrandAttributesRelationDTO == null) {
            //若不存在，新增记录
            CategoryBrandAttributesRelationDTO newDto = new CategoryBrandAttributesRelationDTO();
            newDto.setId(ymlConfig.getPkId());
            newDto.setBrandId(brandId);
            newDto.setCategoryId(categoryId);
            newDto.setUnitIds(unitids);
            this.insert(newDto);
        } else if ("".equals(categoryBrandAttributesRelationDTO.getUnitIds())) {
            CategoryBrandAttributesRelationDTO newDto = new CategoryBrandAttributesRelationDTO();
            newDto.setId(categoryBrandAttributesRelationDTO.getId());
            newDto.setUnitIds(unitids);
            this.updateById(newDto);
        }
    }

    @Override
    public List<CommodityLabelDTO> queryLabelByCategoryIdAndBrandId(Long categoryId, Long brandId) {
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId).eq("brand_id", brandId);
        CategoryBrandAttributesRelationDTO selectOne = this.selectOne(wrapper);
        if (selectOne == null || StringUtils.isBlank(selectOne.getLabelIds())) {
            return null;
        }
        List<CommodityLabelDTO> list = new ArrayList<>();
        String[] labelIds = selectOne.getLabelIds().split(",");
        for (String labelId : labelIds) {
            CommodityLabelDTO select = commodityLabelService.selectById(labelId);
            if (select != null) {
                // 判断标签是否存在商品
                Integer goodsNum = baseMapper.getLabelGoodsNum(select.getId());
                if (goodsNum > 0) {
                    list.add(select);
                }
            }
        }
        return list;
    }
}
